using UnityEngine;

public class Object_WayPoint : MonoBehaviour
{
    [SerializeField] private string transToScene;
    [SerializeField] private bool canBeTriggered = true;
    [SerializeField] private Transform wayPoint;
    [SerializeField] private RespawnType wayPointType;
    [SerializeField] private RespawnType connectedWayPoint;

    private void OnValidate()
    {
        gameObject.name = "Object_WayPoint - " + wayPointType.ToString() + " - " + transToScene;
        if (wayPointType == RespawnType.Enter)
            connectedWayPoint = RespawnType.Exit;
        if (wayPointType == RespawnType.Exit)
            connectedWayPoint = RespawnType.Enter;
    }

    public RespawnType GetWayPointType() => wayPointType;
    public Vector3 GetPositionAndSetTrigger()
    {
        canBeTriggered = false;
        return wayPoint == null ? transform.position : wayPoint.position;
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (canBeTriggered == false)
            return;
        if (collision.GetComponent<Player>() == null)
            return;
        SaveManager.Instance.SaveGame();
        GameManager.Instance.LoadScene(transToScene, connectedWayPoint);
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        canBeTriggered = true;
    }
}
